<template>
  <el-card v-loading="loading">
    <div slot="header" class="clearfix">
      <el-button @click="closeTab()">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </div>
    <el-col :span="24">
      <el-form :model="dataForm" :rules="dataRule" ref="dataForm" :label-width="$i18n.locale === 'en-US' ? '140px' : '160px'">
        <el-col :span="12">
          <!-- 模板编码 -->
          <el-form-item :label="$t('genTableTemplate.templateCode')" prop="templateCode">
            <!--文本框-->
            <el-input v-model="dataForm.templateCode"
                      :placeholder="$t('genTableTemplate.templateCode')" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 模板描述 -->
          <el-form-item :label="$t('genTableTemplate.templateName')" prop="templateName">
            <!--文本框-->
            <el-input v-model="dataForm.templateName"
                      :placeholder="$t('genTableTemplate.templateName')" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 模板文件路径 -->
          <el-form-item :label="$t('genTableTemplate.filePath')" prop="filePath">
            <!--文本框-->
            <el-input v-model="dataForm.filePath"
                      :placeholder="$t('genTableTemplate.filePath')" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 模板文件路径 -->
          <el-form-item :label="$t('genTableTemplate.fileName')" prop="fileName">
            <!--文本框-->
            <el-input v-model="dataForm.fileName"
                      :placeholder="$t('genTableTemplate.fileName')" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 程序类型 -->
          <el-form-item :label="$t('genTableTemplate.appType')" prop="appType">
            <!--下拉框-->
            <el-select v-model="dataForm.appType" style="width: 100%" :placeholder="$t('genTableTemplate.appType')" clearable>
              <el-option v-for="template in $getDictList('GEN_TEMP_APP_TYPE', true)"
                         :key="template.val"
                         :label="template.label"
                         :value="template.val"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 备注信息 -->
          <el-form-item :label="$t('genTableTemplate.fileContent')" prop="fileContent">
            <!--文本域-->
            <el-input type="textarea" :rows="20" v-model="dataForm.fileContent"
                      :placeholder="$t('genTableTemplate.fileContent')"></el-input>
          </el-form-item>
        </el-col>
      </el-form>
    </el-col>
  </el-card>
</template>

<script>
import debounce from 'lodash/debounce'
import mainContent from '@/views/main-content'
var content = mainContent
export default {
  data () {
    return {
      loading: false,
      visible: false,
      dataForm: {
        id: '',
        templateCode: '',
        templateName: '',
        templateFile: '',
        templateType: '',
        filePath: '',
        fileName: '',
        fileContent: '',
        remark: '',
        appType: '',
        creator: '',
        createDate: '',
        updater: '',
        updateDate: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        templateCode: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        templateName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        filePath: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        fileName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        fileContent: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        appType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  activated () {
    // 通过路由参数pid, 控制列表请求操作
    if (this.$route.params.timestamp !== this.timestamp) {
      this.$refs['dataForm'].resetFields()
    }
    if (this.$route.params.id) {
      this.dataForm.id = this.$route.params.id
      this.loading = true
      this.getInfo()
    }
  },
  methods: {
    // 获取信息
    getInfo () {
      this.$http.get(`/gen/genTableTemplate/${this.dataForm.id}`).then(({ data: res }) => {
        this.loading = false
        if (res.code !== 0) {
          return false
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 关闭 tab
    closeTab () {
      content.methods.tabRemoveHandle(this.$route.name)
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/gen/genTableTemplate/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.closeTab()
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
